#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "app_wifi.h"
#include "nvs_flash.h"
#include "driver/i2c_master.h"
#include "esp_now.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "app_espnow.h"
#include "string.h"
#include "esp_mac.h"
#include "app_message_handler.h"

// #define CONFIG_LOG_MAXIMUM_LEVEL 3

static uint8_t broadcast[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const char *TAG = "espnow";
static void app_espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
    if (status == ESP_NOW_SEND_SUCCESS)
    {
        ESP_LOGI(TAG, "Send successful");
    }
    else
    {
        ESP_LOGI(TAG, "Send failed with status");
    }
}

static void app_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len)
{
    ESP_LOGI(TAG, "Received data!");
    app_message_handler_send(data, len);
}
void app_espnow_init()
{
    esp_now_init();
    esp_now_register_send_cb(app_espnow_send_cb);
    esp_now_register_recv_cb(app_espnow_recv_cb);
        esp_now_peer_info_t peer = {
        .channel = 0,
        .ifidx = ESP_IF_WIFI_STA,
        .encrypt = 0,
        .peer_addr = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
    };
    esp_now_add_peer(&peer);
}
void app_espnow_send(char *data, int len){
    esp_now_send(broadcast, (uint8_t *)data, len);
 }